<link type="text/css" href="/admins/js/pagerView/pager-view.css" rel="stylesheet" />
<link type="text/css" href="/admins/js/jquery/css/ui-lightness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="/admins/js/jquery/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="/admins/js/jquery/jquery.ui.datepicker-zh-CN.js"></script>
<script type="text/javascript" src="/admins/js/pagerView/pager-view.js"></script>
<script type="text/javascript">
$(function(){
	$.datepicker.setDefaults( $.datepicker.regional[ "zh-CN" ] );
	$( "#add-button").button().click( function () {
		showDialog(0);
	});
	$( ".button").button().click( function () {
		idList = "";
		sp = "";
		$( ".checkbox-del" ).each(function(){
			if ($(this).attr("checked") == "checked") {
				idList += sp + $(this).val();
				if (sp == "") sp = ",";
			}
		});
		if (idList != "") {
			switch (this.id) {
				case 'del-button':
					alertText = '删除';
					reqUrl = '/system/distribution-del';
					break;
				case 'enable-button':
					alertText = '启用';
					reqUrl = '/system/distribution-enable';
					break;
				case 'disable-button':
					alertText = '禁用';
					reqUrl = '/system/distribution-disable';
					break;								
			}
			if (confirm( "你真的要" +alertText+ "选中的数据吗？" )) {
				$.ajax({
					type: "POST",
					url: reqUrl,
					dataType: "json",
					data:"idList="+idList,
					success: function(data) {
						var result=eval(data);
						if (result == true) {
							$( "#queryButton").click();
							alert( alertText + "成功！" );
						} else {
							alert( alertText + "失败！" );
						}
					}
				});
			}
		} else {
			alert("请选择要操作的数据!");
		}
	});
    $( "#queryButton").button().click( function () {
		$("#typeName2").val($("#typeName").val());
		$("#cityName2").val($("#cityName").val());
		$("#agentName2").val($("#agentName").val());
	    $.ajax({
			type: "POST",
			url: "/system/distribution-query",
			dataType: "json",
			data:"&typeName="+$("#typeName").val()+"&cityName="+$("#cityName").val()+"&agentName="+$("#agentName").val()+"&pagesize="+pager.size,
			success: function(data) {
				var items=eval(data.items);
				bindDataTable(items)
				pager.itemCount = data.count;
				pager.index = 1
				pager.render();
			}
		});

		return false; 
	});
	$( "#starttime" ).datepicker();
	$( "#endtime" ).datepicker();
	var pager = new PagerView('pager');
	pager.size = 15; // 每页显示15条记录
	pager.maxButtons = 10;
	pager.itemCount = 0;
	pager.onclick = function(index) {
		$.ajax({
			type: "POST",
			url: "/system/distribution-pager",
			dataType: "json",
			data:"&typeName="+$("#typeName2").val()+"&cityName="+$("#cityName2").val()+"&agentName="+$("#agentName2").val()+"&pagesize="+pager.size+"&pageindex="+index,
			success: function(data) {
				var items=eval(data);
				bindDataTable(items)
			}
		});
		
	};
	pager.render();

	$( "#queryButton").click();
});
function bindDataTable(items) {
	$(".datatable tbody").empty();
	$.each(items, function(k)
	{
		items[k]['state'] = items[k]['state'] == 0 ? '<font color=\'red\'>&times;</font>' : '<font color=\'green\'>&radic;</font>';
		$(".datatable tbody").append("<tr><td><input type=\"checkbox\" value=\""+items[k]['id']+"\" class=\"checkbox-del\" /></td><td>"+items[k]['id']+"</td><td>"+items[k]['cityName']+"</td><td>"+items[k]['typeName']+"</td><td>"+items[k]['agentName']+"</td><td>"+items[k]['queryurl']+"</td><td>"+items[k]['startweight']+"</td><td>"+items[k]['weight']+"</td><td>"+items[k]['startPrice']+"</td><td>"+items[k]['price']+"</td><td>"+items[k]['oil']+"</td><td>"+items[k]['clearancePrice']+"</td><td>"+items[k]['pushTime']+"</td><td>"+items[k]['state']+"</td><td><img src=\"/admins/images/edit.gif\" id=\""+items[k]['id']+"\" class=\"edit-operation\"/></td></tr>");
	});
	$( ".edit-operation").click( function () {
		showDialog(this.id);
	});
	$( ".datatable tbody tr" ).each(function(){
		$(this).click( function () { 
			$( ".datatable .select" ).removeClass("select");
			$(this).addClass("select"); 
		});
		$(this).hover(
			function () {
				$(this).addClass("hover");
			},
			function () {
				$(this).removeClass("hover");
			}
		);
	});
}
</script>
<form action="/system/distribution" method="post" style="margin:0px">
<div style=" border:1px solid #DDDDDD; padding:2px; margin:2px;">
<label for="name">快递名称：</label><input type="text" id="typeName" name="typeName" />
<label for="name">城市名称：</label><input type="text" id="cityName" name="cityName" />
<label for="name">代理公司：</label><input type="text" id="agentName" name="agentName" />
<input type="submit" id="queryButton" value="查询"/>
</div>
</form>
<style>
	.datatable {
		text-align: left;
		font-size: 12px;
		font-family: verdana;
		background: #c0c0c0; margin:2px;
	}

	.datatable thead tr,

	.datatable tfoot tr {
		background: #c0c0c0;
	}

	.datatable tbody tr {
		background: #f0f0f0;
	}

	.datatable td, th {
		border: 1px solid white; height:28px;
	}
	.datatable td {background-color:white;}
	
	.datatable .hover td {background-color:#99CCFF;}
	
	.datatable .select td {background-color:#EEEE99;}

	#ui-datepicker-div{ font-size:12px;}
</style>
<div style="padding:2px; margin:2px;">
<table cellspacing="1" width="100%" class="datatable">
	<thead>
		<tr>
            <th></th>
			<th>ID</th>
			<th>城市名</th>
			<th>快递名称</th>
			<th>代理公司</th>
			<th>查询地址</th>
			<th>首重</th>
			<th>续重</th>
            <th>首重费</th>
            <th>续重费</th>
            <th>燃油</th>
            <th>报关费</th>
            <th>发布时间</th>
            <th>状态</th>
            <th>操作</th>
		</tr>
	</thead>
    <tfoot>
      <tr><td colspan="15">
        <button id="del-button" class="button">删除选中</button>
        <button id="add-button" >添加</button>
        <button id="enable-button" class="button">启用</button>
        <button id="disable-button" class="button">禁用</button>
      </td></tr>
    </tfoot>
	<tbody>
	</tbody>
</table>
</div>
<form action="/system/distribution-pager" method="post">
<input type="hidden" id="typeName2" name="typeName2" value="" />
<input type="hidden" id="cityName2" name="cityName2" value="" />
<input type="hidden" id="agentName2" name="agentName2" value="" />
</form>
<div id="pager" class="pager"></div>
<style type="text/css">
#dialog-modal { display:none; }
#dialog-modal input { width:170px;}
</style>
<script type="text/javascript">
$(function(){
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-modal" ).dialog({
		height: 300,
		width: 600,
		modal: true,
		autoOpen: false
	});
    $( "#edit-button-ok").button().click( function () {
		id = $("#record-id").val();shipTypeId = $("#shipTypeId").val();agentCompanyId = $("#agentCompanyId").val();
		discount = $("#discount").val();price = $("#price").val();weight = $("#weight").val();
		startweight = $("#startweight").val();startPrice = $("#startPrice").val();cityid = $("#cityid").val();
		queryurl = $("#queryurl").val();oil = $("#oil").val();clearancePrice = $("#clearancePrice").val();
		pushTime = $("#pushTime").val();state = $("#state").val();cmd = $("#opcmd").val();
		cityName = $("#cityid").find("option:selected").text();shipTypeName = $("#shipTypeId").find("option:selected").text();agentName = $("#agentCompanyId").find("option:selected").text();
		$.ajax({
			type: "POST",
			url: "/system/distribution-"+cmd,
			data:"id="+id+"&cityid="+cityid+"&shipTypeId="+shipTypeId+"&agentCompanyId="+agentCompanyId+"&discount="+discount+"&price="+price+"&weight="+weight+"&startweight="+startweight+"&startPrice="+startPrice+"&queryurl="+queryurl+"&oil="+oil+"&clearancePrice="+clearancePrice+"&pushTime="+pushTime+"&state="+state,
			success: function(data) {
				if (data == 'editok') {
					alert( "更新成功！" );
					row = $( ".datatable .select" ).children();
					$(row).eq(2).text(cityName);
					$(row).eq(3).text(shipTypeName);
					$(row).eq(4).text(agentName);
					$(row).eq(5).text(queryurl);
					$(row).eq(6).text(startweight);
					$(row).eq(7).text(weight);
					$(row).eq(8).text(startPrice);
					$(row).eq(9).text(price);
					$(row).eq(10).text(oil);
					$(row).eq(11).text(clearancePrice);
					$(row).eq(12).text(pushTime);
					stateHtml = state == 0 ? '<font color=\'red\'>&times;</font>' : '<font color=\'green\'>&radic;</font>';
					$(row).eq(13).html(stateHtml);
					$( "#dialog-modal" ).dialog( "close" );
				} else if (data == 'addok') {
					$( "#queryButton").click();
					$( "#dialog-modal" ).dialog( "close" );
					alert( "添加成功！" );
				} else {
					alert( "操作失败！" );
				}
			}
		});
	});
	$( "#edit-button-cancel").button().click( function () {
		$( "#dialog-modal" ).dialog( "close" );
	});
});
function showDialog(id) {
	if (id > 0) {
		$("#opcmd").val( 'edit' );
		$.ajax({
			type: "POST",
			url: "/system/distribution-details",
			dataType: "json",
			data:"id="+id,
			success: function(data) {
				var object = eval(data);
				if (object != false) {
					$("#record-id").val( object.id );
					$("#cityid").val( object.cityid );
					$("#shipTypeId").val( object.shipTypeId );
					$("#agentCompanyId").val( object.agentCompanyId );
					$("#discount").val( object.discount );
					$("#price").val( object.price );
					$("#weight").val( object.weight );
					$("#startweight").val( object.startweight );
					$("#startPrice").val( object.startPrice );
					$("#queryurl").val( object.queryurl );
					$("#oil").val( object.oil );
					$("#clearancePrice").val( object.clearancePrice );
					$("#pushTime").val( object.pushTime );
					$("#state").val( object.state );
					$( "#dialog-modal" ).dialog( "open" );
				} else {
					alert( "获取数据失败！" );
				}
			}
		});
	} else {
		$("#opcmd").val( 'add' );
		$("#cityid").val( 1 );
		$("#shipTypeId").val( 1 );
		$("#agentCompanyId").val( 1 );
		$("#discount").val( '' );
		$("#price").val( '' );
		$("#weight").val( '' );
		$("#startweight").val( '' );
		$("#startPrice").val( '' );
		$("#queryurl").val( '' );
		$("#oil").val( '' );
		$("#clearancePrice").val( '' );
		$("#pushTime").val( '' );
		$("#state").val( '' );
		$( "#dialog-modal" ).dialog( "open" );
	}
}
</script>
<div id="dialog-modal" title="操作">
	<table width="100%">
      <tr>
        <td align="right">城市：</td>
        <td>
          <select id="cityid">
          <?php foreach ($this->city_list as $item): ?>
          <option value="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
          <?php endforeach; ?>
         </select>
        </td>
        <td align="right">快递方式：</td>
        <td>
          <select id="shipTypeId">
          <?php foreach ($this->shiptype_list as $item): ?>
          <option value="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
          <?php endforeach; ?>
         </select>
        </td>
      </tr>
      <tr>        
        <td align="right">代理公司：</td>
        <td colspan="3">
          <select id="agentCompanyId">
           <?php foreach ($this->agent_list as $item): ?>
           <option value="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
           <?php endforeach; ?>
          </select>
        </td>
     </tr>
     <tr>
        <td align="right">折扣：</td><td><input id="discount" type="text" value="" /></td>
        <td align="right">燃油：</td><td><input id="oil" type="text" value="" /></td>
     </tr>
     <tr>
       <td align="right">首重：</td><td><input id="startweight" type="text" value="" /></td>
       <td align="right">首重价：</td><td><input id="startPrice" type="text" value="" /></td>
     </tr>
     <tr>
        <td align="right">续重：</td><td><input id="weight" type="text" value="" /></td>
        <td align="right">续重价：</td><td><input id="price" type="text" value="" /></td>
     </tr>
     <tr>
        <td align="right">报关费：</td><td><input id="clearancePrice" type="text" value="" /></td>
        <td align="right">发布时间：</td><td><input id="pushTime" type="text" value="" /></td>
     </tr> 
     <tr>
        <td align="right">查询地址：</td><td><input id="queryurl" type="text" value="" /></td>
        <td align="right">状态：</td>
        <td>
         <select id="state">
          <option value="1">启用</option>
          <option value="0">禁用</option>
         </select>
        </td>
     </tr>          
     <tr>
       <td colspan="4" align="center">
       <button id="edit-button-ok">确认更新</button>
       <button id="edit-button-cancel">取消返回</button>
       <input type="hidden" id="record-id" value="" />
       <input type="hidden" id="opcmd" value="" />
       </td>
     </tr>
    </table>
</div>